Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Data.SqlServerCe
Imports System.Data.OracleClient
Imports System.Data
Imports DTO
Public Class QuyenDao
    Inherits AbstractDao
#Region "New"
    Public Sub New(ByVal connection As KetNoiDto)
        MyBase.New(connection)
    End Sub
    Public Sub New(ByVal ma As Integer, ByVal connection As KetNoiDto)
        MyBase.New("Quyen", "select * from Quyen where IdQuyen=" & ma & "", connection)
    End Sub
#End Region
    Public Sub Them(ByVal Dto As Quyendto)
        Dim strsql As String
        strsql = "Insert into Quyen(TenQuyen) values (?)"
        Moketnoi()
        Select Case Ketnoi.MsSql
            Case Class_KieuDL.Access
                Dim cmd As New OleDbCommand(strsql, Ket_noi_Access)
                cmd.Parameters.Add("@TenQuyen", OleDbType.WChar)
                cmd.Parameters("@TenQuyen").Value = Dto.TenQuyen
                cmd.ExecuteNonQuery()
                strsql = "Select @@IDENTITY"
                cmd = New OleDbCommand(strsql, Ket_noi_Access)
                Dto.IdQuyen = cmd.ExecuteScalar()
            Case Class_KieuDL.sql, Class_KieuDL.sqlExpress
                strsql = "Insert into Quyen(TenQuyen) values (N'" & Dto.TenQuyen & "')"
                Dim cmd1 As New SqlCommand(strsql, Ket_noi_Sql)
                cmd1.ExecuteNonQuery()
                strsql = "Select @@IDENTITY"
                cmd1 = New SqlCommand(strsql, Ket_noi_Sql)
                Dto.IdQuyen = cmd1.ExecuteScalar()
            Case Class_KieuDL.SqlCe
                strsql = "Insert into Quyen(TenQuyen) values ('" & Dto.TenQuyen & "')"
                Dim cmd1 As New SqlCeCommand(strsql, Ket_noi_SqlCe)
                cmd1.ExecuteNonQuery()
            Case Class_KieuDL.Oracle
                strsql = "Insert into Quyen(TenQuyen) values ('" & Dto.TenQuyen & "')"
                Dim cmd1 As New OracleCommand(strsql, Ket_noi_Oracle)
                cmd1.ExecuteNonQuery()
        End Select
        Ngatketnoi()
    End Sub
    Public Sub Xoa(ByVal ma As String)
        Dim strSQL As String
        Moketnoi()
        Select Case Ketnoi.MsSql
            Case Class_KieuDL.Access
                strSQL = "Delete From Quyen Where IdQuyen= ? "
                Dim cmd As New OleDbCommand(strSQL, Ket_noi_Access)
                cmd.Parameters.Add("@ma", OleDbType.Integer)
                cmd.Parameters("@ma").Value = ma
                cmd.ExecuteNonQuery()
            Case Class_KieuDL.sql, Class_KieuDL.sqlExpress
                strSQL = "Delete From Quyen Where IdQuyen=" & ma & ""
                Dim cmd1 As New SqlCommand(strSQL, Ket_noi_Sql)
                cmd1.ExecuteNonQuery()
            Case Class_KieuDL.SqlCe
                strSQL = "Delete From Quyen Where IdQuyen=" & ma & ""
                Dim cmd1 As New SqlCeCommand(strSQL, Ket_noi_SqlCe)
                cmd1.ExecuteNonQuery()
            Case Class_KieuDL.Oracle
                strSQL = "Delete From Quyen Where IdQuyen=" & ma & ""
                Dim cmd1 As New OracleCommand(strSQL, Ket_noi_Oracle)
                cmd1.ExecuteNonQuery()
        End Select
        Ngatketnoi()
    End Sub
    Public Sub sua(ByVal dto As Quyendto)
        Dim strSQL As String
        Moketnoi()
        Select Case Ketnoi.MsSql
            Case Class_KieuDL.Access
                strSQL = "Update Quyen Set TenQuyen =?  where IdQuyen= ? "
                Dim cmd As New OleDbCommand(strSQL, Ket_noi_Access)
                cmd.Parameters.Add("@TenQuyen", OleDbType.WChar)
                cmd.Parameters.Add("@IdQuyen", OleDbType.Integer)
                cmd.Parameters("@TenQuyen").Value = dto.TenQuyen
                cmd.Parameters("@IdQuyen").Value = dto.IdQuyen
                cmd.ExecuteNonQuery()
            Case Class_KieuDL.sql, Class_KieuDL.sqlExpress
                strSQL = "Update Quyen Set TenQuyen = N'" & dto.TenQuyen & "' where IdQuyen=" & dto.IdQuyen & ""
                Dim cmd1 As New SqlCommand(strSQL, Ket_noi_Sql)
                cmd1.ExecuteNonQuery()
            Case Class_KieuDL.SqlCe
                strSQL = "Update Quyen Set TenQuyen = '" & dto.TenQuyen & "' where IdQuyen=" & dto.IdQuyen & ""
                Dim cmd1 As New SqlCeCommand(strSQL, Ket_noi_SqlCe)
                cmd1.ExecuteNonQuery()
            Case Class_KieuDL.Oracle
                strSQL = "Update Quyen Set TenQuyen = '" & dto.TenQuyen & "' where IdQuyen=" & dto.IdQuyen & ""
                Dim cmd1 As New OracleCommand(strSQL, Ket_noi_Oracle)
                cmd1.ExecuteNonQuery()
        End Select
        Ngatketnoi()
    End Sub
    Public Sub layBang()
        Chuoi_SQL = " Select * from QUYEN"
        Doc_bang()
    End Sub
    Public Sub layBangTheoIdQuyen(ByVal ARRAY1 As ArrayList)

        Dim DK As String = ""
        If ARRAY1.Count > 0 Then
            For I As Integer = 0 To ARRAY1.Count - 1
                DK = DK & IIf(DK <> "", " OR ", "") & "IdQuyen=" & ARRAY1.Item(I) & ""
            Next
        End If
        If DK <> "" Then DK = " WHERE " & DK
        Chuoi_SQL = " Select * from Nhanvien " & DK
        Doc_bang()
    End Sub
    Public Sub layBangTheoIdQuyen(ByVal IdQuyen As System.Int32)

        Chuoi_SQL = " Select * from Quyen where  IdQuyen = " & IdQuyen & ""
        Doc_bang()
    End Sub
    Public Sub XOABangTheoIdQuyen(ByVal IdQuyen As System.Int32)
        Dim strsql As String
        strsql = "Delete From Quyen Where IdQuyen=" & IdQuyen & ""
        Thuc_hien_lenh(strsql)
    End Sub
    Public Sub layBangTheoTenQuyen(ByVal ARRAY1 As ArrayList)

        Dim DK As String = ""
        If ARRAY1.Count > 0 Then
            For I As Integer = 0 To ARRAY1.Count - 1
                DK = DK & IIf(DK <> "", " OR ", "") & "TenQuyen='" & ARRAY1.Item(I) & "'"
            Next
        End If
        If DK <> "" Then DK = " WHERE " & DK
        Chuoi_SQL = " Select * from Nhanvien " & DK
        Doc_bang()
    End Sub
    Public Sub layBangTheoTenQuyen(ByVal TenQuyen As System.String)

        Chuoi_SQL = " Select * from Quyen where  TenQuyen  like '%" & TenQuyen & "%'"
        Doc_bang()
    End Sub
    Public Function MaQuyen(ByVal TenQuyen As System.String) As String
        Chuoi_SQL = " Select MaQuyen from Quyen where  TenQuyen  = '" & TenQuyen & "'"
        Doc_bang()
    End Function
    Public Sub XOABangTheoTenQuyen(ByVal TenQuyen As System.String)
        Dim strsql As String
        strsql = "Delete From Quyen Where TenQuyen='" & TenQuyen & "'"
        Thuc_hien_lenh(strsql)
    End Sub
    Public Sub layBangNhieuTable()
        Chuoi_SQL = " Select IdQuyen ,TenQuyen  from Quyen"
        Doc_bang()
    End Sub
    Public Sub TaoTable()
        Dim strsql As String
        strsql = ""
        Select Case Ketnoi.MsSql
            Case Class_KieuDL.Access
            Case Class_KieuDL.sql, Class_KieuDL.sqlExpress
                strsql = ""
            Case Class_KieuDL.SqlCe
                strsql = ""
            Case Class_KieuDL.Oracle
                strsql = ""
            Case Else
                strsql = ""
        End Select
        Thuc_hien_lenh(strsql)
    End Sub
End Class

